package com.zhsm.fuxi.mvp.presenter

import android.os.CountDownTimer
import cn.jpush.android.api.JPushInterface
import com.hankkin.library.mvp.presenter.RxLifePresenter
import com.hankkin.reading.control.UserControl
import com.hankkin.reading.http.HttpClientUtils
import com.zhsm.fuxi.app.MyApplication
import com.zhsm.paipaiout.baselibrary.utils.ToastUtils
import com.zhsm.fuxi.mvp.contract.LoginContract
import com.zhsm.fuxi.utils.Base64Utils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * Created by GuZhC on 2018/10/9.
 * describe:
 */
class LoginPresenter : RxLifePresenter<LoginContract.IView>(), LoginContract.IPresenter {
    private  var mTimer: CountDownTimer? =null

    override fun sendImgCode(phone: String) {
        getMvpView().showLoding()
        HttpClientUtils.Builder.getPaipiaHttp().getImageCode(phone)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().hideLoding()
                    getMvpView().setImageCodeUrl(Base64Utils.getFromBase64(it))
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)

    }

    override fun sendSmsCode(phone: String, imageCode: String) {
        getMvpView().showLoding()
        val map = HashMap<String, Any>()
        map["mobile"] = phone
        map["captcha"] = imageCode
        HttpClientUtils.Builder.getPaipiaHttp().sendSmsCode(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().hideLoding()
                    startCountdown()
                    ToastUtils.makeText("验证码已发送")
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }

    override fun Login(phone: String, code: String) {
        getMvpView().showLoding()
        val map = HashMap<String, Any>()
        map.put("mobile", phone)
        map.put("captcha", code)
        HttpClientUtils.Builder.getPaipiaHttp().userLogin(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    UserControl.setCurrentUser(it)
                    postRegisterId(it.id!!)
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }

    override fun postRegisterId(id:Long) {
        getMvpView().showLoding()
        val map = HashMap<String, Any>()
        map.put("customerId", id)
        map.put("registrationId", JPushInterface.getRegistrationID(MyApplication.instance().applicationContext))
        HttpClientUtils.Builder.getPaipiaHttp().postRegisterId(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeEx({
                    getMvpView().loginSucces()
                }, {
                    getMvpView().hideLoding()
                }).bindRxLifeEx(RxLife.ON_DESTROY)
    }


    private fun startCountdown() {
        mTimer = object : CountDownTimer((1000 * 60).toLong(), 1000) {
            override fun onTick(millisUntilFinished: Long) {
                if (getMvpView() == null) return
                getMvpView().setCountDownText((millisUntilFinished / 1000).toInt())
            }

            override fun onFinish() {
                getMvpView().setCountDownText(0)
            }
        }
        mTimer?.start()
    }

    override fun onDestroy() {
        super.onDestroy()
        if (mTimer != null) {
            mTimer?.cancel()
        }
    }
}